<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Comment extends \app\BaseModel
{
    protected $name = "massage_service_order_comment";
    protected $append = ["lable_text", "order_goods"];
    public function getOrderGoodsAttr($value, $data)
    {
        if(!empty($data["order_id"]) && isset($data["id"])) {
            $order_goods_model = new OrderGoods();
            $comment_goods = new CommentGoods();
            $dis = ["order_id" => $data["order_id"]];
            $list = $order_goods_model->where($dis)->select()->toArray();
            if(!empty($list)) {
                foreach ($list as &$value) {
                    $dis = ["service_id" => $value["goods_id"], "comment_id" => $data["id"]];
                    $info = $comment_goods->dataInfo($dis);
                    if(!empty($info)) {
                        $value["star"] = $info["star"];
                    }
                }
            }
            return $list;
        }
    }
    public function getLableTextAttr($vaule, $data)
    {
        if(!empty($data["id"])) {
            $lable_model = new Lable();
            $dis = ["b.comment_id" => $data["id"], "a.status" => 1];
            $list = $lable_model->alias("a")->join("massage_service_comment_lable b", "a.id = b.lable_id")->where($dis)->column("a.title");
            return array_values($list);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_order_list b", "a.order_id = b.id", "left")->join("massage_service_order_goods_list c", "a.order_id = c.order_id", "left")->join("massage_service_coach_list d", "a.coach_id = d.id", "left")->join("massage_service_user_list e", "a.user_id = e.id", "left")->where($dis)->field("a.*,b.order_code,e.nickName,e.avatarUrl,c.goods_name,c.goods_cover,c.num,c.price,d.coach_name")->group("a.id")->order("a.is_good desc,a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function adminDataList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_order_list b", "a.order_id = b.id", "left")->join("massage_service_order_goods_list c", "a.order_id = c.order_id", "left")->join("massage_service_coach_list d", "a.coach_id = d.id", "left")->join("massage_service_user_list e", "a.user_id = e.id", "left")->where($dis)->field("a.*,b.order_code,e.nickName,e.avatarUrl,c.goods_name,c.goods_cover,c.num,c.price,d.coach_name")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function updateStar($coach_id)
    {
        $all_count = $this->where(["coach_id" => $coach_id])->where("status", ">", -1)->count();
        $all_star = $this->where(["coach_id" => $coach_id])->where("status", ">", -1)->sum("star");
        $now_star = 0 < $all_count ? round($all_star / $all_count, 1) : 5;
        $now_star = 5 < $now_star ? 5 : $now_star;
        $coach_model = new Coach();
        $coach_model->dataUpdate(["id" => $coach_id], ["star" => $now_star]);
        return true;
    }
}

?>